﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Mock1G3.BaseEntities;
using Mock1G3.BussinessLogic;

namespace Mock1G3.UserControl.UCChangePin
{
    public partial class ChangePin : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnCheck_OnClick(object sender, EventArgs e)
        {
            if (txtPIN1.Visible == true)
            {
                if (txtPIN1.Text == "")
                {
                    Label2.Text = "Numeric new PIN is not empty".ToString();
                }
                else
                {
                    string txtPin = txtPIN1.Text;
                    if (txtPin.Length == 6 & txtPin != Session["PIN"].ToString())
                    {
                        Label2.Text = "Re-Enter PIN you want to change";
                        txtPIN1.Visible = false;
                        txtPIN2.Visible = true;
                        Session.Add("Pin1", txtPIN1.Text);
                    }
                    else
                    {
                        Label2.Text = "New PIN your have just entered not match";
                    }
                }
            }
            else
            {
                if (txtPIN2.Text == "")
                {
                    Label2.Text = "Numeric PIN is not empty";
                }
                else
                {
                    string txtPin = txtPIN2.Text;
                    if (txtPin == Session["Pin1"].ToString())
                    {
                        Thread.Sleep(TimeSpan.FromSeconds(2));
                        Card card = new Card();
                        card.CardNo = Session["CardNo"].ToString();
                        card.PIN = GetMD5(txtPin);
                        Log log = new Log();
                        log.ATMID = int.Parse(Session["ATMID"].ToString());
                        log.CardNo = Session["CardNo"].ToString();
                        log.LogDate = DateTime.Now;

                        if (BussinessLogic.ValidateLogic.UpdatePin(card) == true & BussinessLogic.ValidateLogic.WriteLog(log) == true)
                        {
                            Label2.Text = "Your PIN Changed!\n Eject card!";
                            txtPIN2.Visible = false;
                            Session.RemoveAll();
                        }
                        else
                        {
                            Label2.Text = "Can not your PIN change";
                        }
                        //Response.Redirect("Default.aspx");
                    }
                    else
                    {
                        Label2.Text = "new PIN your have just entered not match";
                    }
                }
            }
        }

        protected void btnClear_OnClick(object sender, EventArgs e)
        {
            if (txtPIN1.Visible == true)
            {
                txtPIN1.Text = "";
                txtPIN1.Focus();
            }
            else
            {
                txtPIN2.Text = "";
                txtPIN2.Focus();
            }
        }

        protected void btnExit_Click(object sender, EventArgs e)
        {
            Session.RemoveAll();
            Response.Redirect("Default.aspx");
        }

        public static string GetMD5(string str)
        {

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] bHash = md5.ComputeHash(Encoding.UTF8.GetBytes(str));

            StringBuilder sbHash = new StringBuilder();

            foreach (byte b in bHash)
            {
                sbHash.Append(String.Format("{0:x2}", b));
            }
            return sbHash.ToString();

        }
    }
}